<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/jq.autocomplete.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.autocomplete.js"></script>
<script type="text/javascript">
sort = function(){
	$("ul.product").sortable({
		connectWith: "ul",
	});
};
select = function(){
	$("ul.product").selectable({});
};
Refresh = function(){
	//jq的自动完成
	$(".ajax_product").autocomplete("<?=url('Product::Image/ProductInfoAjax')?>").result(function(event, data, formatted){
		if (data[0])
		{
			var _html = data[0] + "<input type='hidden' name='product_id_" + data['id'] + "' value='" + data['id'] + "' />";
			current = $(this).parent();
			$(this).remove();
			current.addClass("ui-widget-content").html(_html);
		}
	});
};
$(function(){
	sort();
	select();
	$("ul.product>li.ui-widget-content").live('click', function(){
		if ($(this).is(".ui-selected"))
		{
			$(this).removeClass("ui-selected");
		}
		else
		{
			$(this).addClass("ui-selected");
		}
	});
	var group_count = <?=count($requirement['group'])?>;
	$(".product_add_group").live("click", function(){
		var _html = "<tr class='tr_product'>";
		_html += "<td>" + (group_count + 1) + "</td>";
		_html += "<td><input type='text' name='group_add" + group_count + "_note' size='80' /><td><a href='javascript: void(0)' class='del_group'>删除组</a> <a href='javascript: void(0)' class='add_product'>添加产品</a></td>";
		_html += "</tr>";
		_html += "<tr class='tr_product'><td></td><td colspan='2'>";
		_html += "	<table class='list_table'>"
		_html += "		<tr><td><ul class='product' id='ul_product" + group_count + "'></ul></td><td width='100px'><a href='javascript: void(0)' class='product_add_group'>新增组</a> <a href='javascript: void(0)' class='del_product'>删除产品</a></td></tr>"
		_html += "  </table>";
		_html += "</td></tr>";
		$("#list").append(_html);
		$(".product>.ui-selected").removeClass("ui-selected").appendTo($("#ul_product" + group_count));
		sort();
		select();
		group_count++;
	});
	$(".del_group").live("click", function(){
		if ( ! confirm("确定删除该组？"))
		{
			return false;
		}
		else
		{
			if ($(this).parent().parent().next().is(".tr_product"))
			{
				$(this).parent().parent().next().find("ul.product>li").appendTo($("#ul_product0"));
				$(this).parent().parent().next().remove();
			}
			$(this).parent().parent().remove();
		}
	});
	$(".add_product").live("click", function(){
		var _html = "<li class='ui-state-default'><input type='text' size='48' class='ajax_product' value='' /></li>";
		$(this).parent().parent().next().find("ul").append(_html);
		Refresh();
	});
	$(".del_product").live("click", function(){
		selected = $(this).parent().prev().find(".product>.ui-selected");
		if (selected.length == 0)
		{
			alert("请选择要删除的产品！！");
			return false;
		}
		if ( ! confirm("确定要删除选中产品？"))
		{
			return false;
		}
		else
		{
			selected.remove();
		}
	});
});
</script>
<style>
ul.product {list-style-type: none; background: #eee; padding: 5px; width: 540px;}
ul.product li {margin: 5px auto; padding: 5px; font-size: 12px; width: 460px; cursor: default;}
ul.product .ui-selecting {background: #FECA40;}
ul.product .ui-selected {background: #F39814;}
</style>
<h2>新增产品图片需求单</h2>
<form action="<?=url('Product::Image/EditSave')?>" method="post">
<table width="" class="form_table">
<tr>
	<th>类型</th>
	<td width="100px">
	<select name="type">
		<?foreach (Q::ini('custom_flag/image_requirement_type') as $t):?>
		<option value="<?=$t['value']?>" <?=$t['value'] == $requirement['type'] ? 'selected="selected"' : ''?>><?=$t['name']?></option>
		<?endforeach;?>
	</select>
	</td>
	<th width="100px">需求人员</th>
	<td>
	<select name="requirement_user_id">
		<?foreach ($user as $u):?>
		<option value="<?=$u['id']?>" <?=$u['id'] == $requirement['requirement_user_id'] ? 'selected="selected"' : ''?>><?=$u['name']?></option>
		<?endforeach;?>
	</select>
	</td>
</tr>
<tr>
	<th>名称</th>
	<td colspan="3"><input type="text" name="name" size="80" value="<?=$requirement['name']?>" /></td>
</tr>
<tr>
	<th>备注</th>
	<td colspan="3"><textarea rows="8" cols="112" name="note"><?=$requirement['note']?></textarea></td>
</tr>
</table>
<table width="65%" class="list_table" id="list">
<tr>
	<th width="50px">图片组</th>
	<th>组备注</th>
	<th width="110px">操作</th>
</tr>
<?foreach ($requirement['group'] as $key => $rg):?>
<tr>
	<td><?=$key + 1?></td>
	<td><input type="text" name="group_<?=$rg['id']?>_note" size="80" value="<?=$rg['note']?>" /></td>
	<td>
		<a href="javascript: void(0)" class="del_group" >删除组</a>
		<a href="javascript: void(0)" class="add_product">添加产品</a>
	</td>
</tr>
<tr class="tr_product">
	<td></td>
	<td colspan="2">
		<table class="list_table">
		<tr>
			<td>
			<ul class="product" id="ul_product<?=$key?>">
			<?foreach ($rg['item'] as $rgi):?>
				<li class="ui-state-default ui-widget-content">
					<?=$rgi['product_number'] . ' ' . $rgi['product_name']?>
					<input type="hidden" name="product_id_<?=$rgi['id']?>" value="<?=$rgi['product_basic_id']?>" />
				</li>
			<?endforeach;?>
			</ul>
			</td>
			<td width="100px">
				<a href="javascript: void(0)" class="product_add_group">新增组</a>
				<a href="javascript: void(0)" class="del_product">删除产品</a>
			</td>
		</tr>
		</table>
	</td>
</tr>
<?endforeach;?>
</table>
<div align="center" style="width: 65%">
	<input type="hidden" name="requirement_id" value="<?=$requirement['id']?>" />
	<input type="submit" class="button" value="保存" />
</div>
</form>
<?php $this->_endblock(); ?>